package com.itheima.web.controller.system;

import com.itheima.core.BaseServlet;
import com.itheima.core.ResponseBody;
import com.itheima.domain.system.Dept;
import com.itheima.service.system.DeptService;
import com.itheima.service.system.impl.DeptServiceImpl;
import com.itheima.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/system/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService=new DeptServiceImpl();
    public Object list(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String pageNum = request.getParameter("pageNum");
        if(StringUtil.isEmpty(pageNum)){
            pageNum="1";
        }
        String pageSize = request.getParameter("pageSize");
        if(StringUtil.isEmpty(pageSize)){
            pageSize="10";
        }
        PageInfo<Dept> pageInfo = deptService.pageQuery(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        request.setAttribute("page",pageInfo);
        return "/WEB-INF/pages/system/dept/list.jsp";
    }
    public Object toAdd(HttpServletRequest request, HttpServletResponse response)throws Exception{
        List<Dept> list = deptService.findAll();
        request.setAttribute("deptList",list);
        return "/WEB-INF/pages/system/dept/add.jsp";
    }
    public Object save(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Dept dept = BeanUtil.fillBean(request, Dept.class);
        deptService.save(dept);
        return "/system/dept/list";
    }
    public Object toEdit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String id = request.getParameter("id");
        Dept byId = deptService.findById(id);
        List<Dept> list = deptService.findAll();
        request.setAttribute("dept",byId);
        request.setAttribute("deptList",list);
        return "/WEB-INF/pages/system/dept/update.jsp";
    }
    public Object edit(HttpServletRequest request, HttpServletResponse response)throws Exception{
        Dept dept = BeanUtil.fillBean(request, Dept.class);
        deptService.update(dept);
        return "/system/dept/list";
    }
    public Object delete(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String[] ids = request.getParameterValues("id");
        deptService.delete(ids);
        return "/system/dept/list";
    }

    public Object findByName(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String pageNum = request.getParameter("pageNum");
        if (StringUtil.isEmpty(pageNum)){
            pageNum = "1";
        }
        String pageSize = request.getParameter("pageSize");

        if (StringUtil.isEmpty(pageSize)){
            pageSize = "20";
        }
        String deptName = request.getParameter("deptName");



        PageInfo<Dept> pageInfo = deptService.findByName(Integer.parseInt(pageNum), Integer.parseInt(pageSize),deptName);

        request.setAttribute("page",pageInfo);
        return "WEB-INF/pages/system/dept/list.jsp";


    }
}
